package edu.bit.water.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import edu.bit.water.entity.Column;
import edu.bit.water.entity.MetaEvaCtestSum;
import edu.bit.water.entity.MetaEvaWuseInfo;
import edu.bit.water.service.EntityEvaCtestSumService;
import edu.bit.water.service.MetaEvaCtestSumService;
import edu.bit.water.util.ColumnUtil;

@Controller
@RequestMapping("/entity_eva_ctest_sum")
public class EntityEvaCtestSumController {

	@Autowired
	private EntityEvaCtestSumService entityEvaCtestSumService;
	@Autowired
	private MetaEvaCtestSumService metaEvaCtestSumService;
	
	/**
	 * 獲取動態變化列名（水質部分）
	 * @return 列名組
	 */
    @ResponseBody
    @RequestMapping("/selectColumn")
	public String selectColumn() {
		List<Map<String, String>> fields = new ArrayList<>();
		Map<String, String> field = new HashMap<>();
		
		for (MetaEvaCtestSum column : metaEvaCtestSumService.selectColumnAll()) {
			field = new HashMap<>();
			//反馈属性
			field.put("field", column.getEnName().toUpperCase());
			field.put("title", column.getCsName());
			field.put("dataType", column.getDataType().toUpperCase());
			field.put("unit", column.getUnit());
			fields.add(field);
		}
		return JSON.toJSONString(fields);
	}
    
    /**
     * 插入數據
     * @param list 前端传递过来的数据(所有字段除id)
     * @return 返回操作後信息
     */
    @ResponseBody
    @RequestMapping("/insert")
    public Map<String, Object> insertEntityEvaCtestSum(@RequestBody List<Object> list) {
    	List<String> lineColumn = ColumnUtil.columnToEnName(metaEvaCtestSumService.selectColumn());//查詢可變字段
    	return entityEvaCtestSumService.insertEntityEvaCtestSum(list, lineColumn);   	
    }
    
    /**
     * 刪除數據
     * @param listId 删除数据行对应的id
     * @return 操作後信息
     */
    @ResponseBody
    @RequestMapping("/delete")
    public Map<String, Object> deleteEntityEvaCtestSum(@RequestBody List<Integer> listId) {
    	Map<String, Object> modelMap = new HashMap<>();
    	if(entityEvaCtestSumService.deleteEntityEvaCtestSum(listId) == 1) {
    		modelMap.put("status", "success");
    	} else {
    		modelMap.put("status", "failed");
    	}
    	return modelMap;
    }
    
    /**
     * 根據label_id 查詢數據
     * @param label_id 標籤序號
     * @return 評審日期所有記錄數據
     */
    @ResponseBody
    @RequestMapping(value = "/select", method = RequestMethod.GET)
    private Map<String, Object> selectEntityEvaCtestSum(Integer label_id) {
    	Map<String, Object> modelMap = new HashMap<>();
    	try {
    		List<Map<String, Object>> entityEvaCtestSumList = entityEvaCtestSumService.selectEntityEvaCtestSum(label_id);
        	modelMap.put("rows", entityEvaCtestSumList);
    	}catch(Exception e) {
    		modelMap.put("status", "failed");
    		modelMap.put("msg", e.getMessage());
    	}
    	
    	return modelMap;
    }
    
    /**
     * 更新數據
     * @param list 前端返回數據
     * @return 操作後信息
     */
    @ResponseBody
    @RequestMapping("/update")
    public Map<String, Object> updateEntityEvaCtestSum(@RequestBody List<Object> list) {
    	
    	int id = (int) list.remove(0);
    	List<String> lineColumn = ColumnUtil.columnToEnName(metaEvaCtestSumService.selectColumn());
    	
    	return entityEvaCtestSumService.updateEntityEvaCtestSum(id, list, lineColumn);
    }
}
